package org.classg.edu.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.classg.edu.entity.Chapter;
import org.classg.edu.service.ChapterService;
import org.classg.edu.vo.ChapterVo;
import org.classg.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-05-18
 */
@Api(description = "章节管理")
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
public class ChapterController {

    @Autowired
    private ChapterService chapterService;

    @ApiOperation(value = "根据课程id查询章节和小节信息")
    @GetMapping("/queryChapterAndVideoByCourseId/{courseId}")
    public R queryChapterAndVideoByCourseId(@ApiParam(name = "courseId", value = "课程Id", required = true) @PathVariable String courseId) {
        List<ChapterVo> chapterVos = chapterService.queryChapterAndVideoByCourseId(courseId);
        return R.ok().data("chapterVos", chapterVos);
    }

    @ApiOperation(value = "新增章节")
    @PostMapping("/insertChapter")
    public R insertChapter(@ApiParam(name = "chapter", value = "章节对象", required = true) @RequestBody Chapter chapter) {
        chapterService.save(chapter);
        return R.ok();
    }

    @ApiOperation(value = "根据id查询章节")
    @GetMapping("/queryChapterById/{id}")
    public R queryChapterById(@ApiParam(name = "id", value = "章节id", required = true) @PathVariable String id) {
        Chapter chapter = chapterService.getById(id);
        return R.ok().data("chapter", chapter);
    }

    @ApiOperation("根据章节对象更新数据")
    @PutMapping("/updateChapter")
    public R updateChapter(@ApiParam(name = "chapter", value = "章节对象", required = true) @RequestBody Chapter chapter) {
        chapterService.updateById(chapter);
        return R.ok();
    }

    @ApiOperation(value = "根据id删除章节")
    @DeleteMapping("/deleteChapterById/{id}")
    public R deleteChapterById(@ApiParam(name = "id", value = "章节id", required = true) @PathVariable String id) {
        Boolean result = chapterService.deleteChapterById(id);
        if (result) {
            return R.ok();
        } else {
            return R.error().message("删除章节失败");
        }
    }
}

